package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.pojo.Course;
import com.service.CourseStyleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller

public class CourseStyleController {

    @Autowired
    private CourseStyleService service;

    //    查询和模糊查询
    @RequestMapping("/showCourseStyleList")
    public String showCourseStyleList(Model model, Integer page, String cName, String fukuan) {
        if (page == null) {
            page = 1;
        }
        //设置分页
        PageHelper.startPage(page, 10);
//        查询全部 模糊查询
        List<Course> list = service.findCourseStyleCname(cName,fukuan);
//        List<Course> list = service.showList();
        System.out.println("list1集合" + list);
        PageInfo<Course> pageInfo = new PageInfo<Course>(list, 5);
        model.addAttribute("pageInfo", pageInfo);
        if (cName == null && fukuan == null) {
            return "courseStyle";
        }
        model.addAttribute("cName", cName);
        model.addAttribute("fukuan", fukuan);
        System.out.println("分页集合" + pageInfo);
        System.out.println("需要查询的内容" + cName);
        System.out.println("上下架" + fukuan);
        return "courseStyle";
    }



    @RequestMapping("/updateCourseStyle")

    public String updateCourseStyle(Course course){
        System.out.println("修改课程表显示"+course);
        boolean b = service.updateCourseStyle(course);
        if(b=true){
            return "redirect:/showCourseStyleList";
        }else {
            return "redirect:/showCourseStyleList";
        }
    }

//    查一个
    @RequestMapping("findCourseStyleById")
    public String findCourseStyleById(Model model,Integer cId){
        System.out.println(cId);
        Course showById = service.findCourseStyleById(cId);
        System.out.println("查一个显示"+showById);
        model.addAttribute("showById",showById);
        return "courseStyleView";
    }

    @RequestMapping("findUpdateStyleById")
    public String findUpdateStyleById(Model model,Integer cId){
        System.out.println(cId);
        Course showById = service.findCourseStyleById(cId);
        System.out.println("查一个显示1"+showById);
        model.addAttribute("update",showById);
        return "courseStyleUpdate";
//        return "redirect:/showList";
//        return "courseClassification";
    }

//    删除
    @RequestMapping("/deleteCourseStyle")
    @ResponseBody
    public String deleteCourseStyle(Integer cId){
        boolean b = service.deleteCourseStyle(cId);
        System.out.println("选择的删除序号"+cId);
        if(b=true){
            return "1";
        }else {
            return "2";
        }
    }

//   增加
    @RequestMapping("/addCourseStyle")
    public String addCourseStyle(Course course){

        boolean b = service.addCourseStyle(course);
        System.out.println("增加的对象"+course);

        if (b=true){
            return "redirect:/showCourseStyleList";
        }
        return "redirect:/showCourseStyleList";
    }

}
